/**
 * Created with IntelliJ IDEA.
 * User: skyline{http://my.oschina.net/skyline520}
 * Date: 12-11-22
 * Time: 下午9:30
 * To change this template use File | Settings | File Templates.
 */
Ext.define('Airways.controller.system.roleoperation.RoleOperationManageController',{
    extend:'Ext.app.Controller',
    models:['system.role.RoleManageModel'],
    stores:['system.role.RoleManageStore','system.roleoperation.OperationManageStore'],
    views:['system.roleoperation.RoleOperationManageView'],
    refs:[
        {ref:'roleOperationManageView',selector:'roleOperationManageView'},
        {ref:'roleOperationRoleGridPanel',selector:'roleOperationRoleGridPanel'},
        {ref:'roleOperationRoleQueryFormPanel',selector:'roleOperationRoleQueryFormPanel'},
        {ref:'roleOperationLeftView',selector:'roleOperationLeftView'},
        {ref:'roleOperationRightView',selector:'roleOperationRightView'}
    ],
    init:function (){
        var controller = this;
        this.control({
             'roleOperationManageView':{
                 afterrender:function(){
                 }
             },
            'roleOperationRoleQueryFormPanel button[action=query]':{
                click:function(){
                    controller.getSystemRoleRoleManageStoreStore().loadPage(1);
                }
            },
            'roleOperationRoleGridPanel':{
                render:function(grid,opts){
                    var store=grid.getStore();
                    var _queryFormPanel = controller.getRoleOperationRoleQueryFormPanel();
                    var button = _queryFormPanel.down("button[action=query]")
                    store.on({
                        load: function(){
                            button.enable();
                        },
                        beforeload:function(){
                            controller.record = null;
                            button.disable();
                            store.proxy.extraParams = _queryFormPanel.getForm().getFieldValues();
                        }
                    })
                },
                selectionchange: function(view, records) {
                    if (records[0]) {
                        controller.record = records[0];
                        controller.getSystemRoleoperationOperationManageStoreStore().load();
                    }
                }
            },
            'roleOperationRightView':{
                render:function(grid,opts){
                    var store=grid.getStore();
                    store.on({
                        load: function(){
                            controller.getRoleOperationRightView().expandAll();
                        },
                        beforeload:function(){
                            store.proxy.extraParams = {
                                roleId:controller.record.data.roleId
                            }
                        }
                    })
                },
                itemexpand:function(node,eOpts){
                    node.set({iconCls:node.data.expandedCls});
                },
                itemcollapse:function(node,eOpts){
                    node.set({iconCls:node.data.collapsedCls});
                },
                checkchange:function (node,checked,eOpts){
                    //选中事件
                    setChildChecked(node,checked);
                    setParentChecked(node,checked);
                }
            },
            'roleOperationRightView button[action=save]':{
                click:function(){
                    var re = controller.record;
                    if(re == null){
                        Ext.MessageBox.show({title:'提示',msg:'请先选择角色数据!',icon:Ext.MessageBox.WARNING,buttons:Ext.MessageBox.OK});
                        return;
                    }
                    var records = controller.getRoleOperationRightView().getView().getChecked();
                    var ids = [];
                    Ext.Array.each(records, function(rec){
                        ids.push(rec.raw.operationId);
                    });
                    var url = basePath+'system/roleoperation/saveRoleOperation';
                    var params = {};
                    params.roleId = re.data.roleId;
                    params.version = re.data.version;
                    params.ids = ids.join(',');
                    //调用ajax 发送数据到后台
                    callAjax(url,params,this.handleResult);
                }
            },
            handleResult:function(){
            }
        });
        function setChildChecked(node,checked){
            node.expand();
            node.set({checked:checked});
            if(node.hasChildNodes()){
                node.eachChild(function(child) {
                    setChildChecked(child,checked);
                });
            }
        }
        function setParentChecked(node,checked){
            node.set({checked:checked});
            var parentNode = node.parentNode;
            if(parentNode !=null){
                var flag = false;
                parentNode.eachChild(function(child) {
                    if(child.data.checked == true){
                        flag = true;
                    }
                });
                if(checked == false){
                    if(!flag){
                        setParentChecked(parentNode,checked);
                    }
                }else{
                    if(flag){
                        setParentChecked(parentNode,checked);
                    }
                }
            }
        }
    }
});
